<?php

namespace App\Admin\Components;

use App\Models\OwnerItem;
use Carbon\Carbon;
use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Metrics\Bar;
use Illuminate\Http\Request;

class HouseSessions extends Bar
{
    /**
     * 初始化卡片内容
     */
    protected function init()
    {
        parent::init();

        $color = Admin::color();

        $dark35 = $color->dark35();

        // 卡片内容宽度
        $this->contentWidth(5, 7);
        // 标题
        $this->title('最近7天收入');
        // 设置下拉选项
        $this->subTitle('最近7天');
        // 设置图表颜色
        $this->chartColors([
            $dark35,
            $dark35,
            $color->primary(),
            $dark35,
            $dark35,
            $dark35
        ]);
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {

        $income = OwnerItem::query()->where('pay_at', ">", Carbon::now()->subDays(7))->sum('total_amount');
        $oldIncome = OwnerItem::query()->where('pay_at', ">", Carbon::now()->subDays(14))
            ->where('pay_at', "<", Carbon::now()->subDays(7))
            ->sum('total_amount');
        if ($oldIncome == 0) {
            $this->withContent($income, '100%');
        } else {
            $this->withContent($income, (int)($income / $oldIncome));
        }
        $data[] = OwnerItem::query()->whereDate('pay_at', "=", Carbon::now()->toDate())->sum('total_amount');
        $data[] = OwnerItem::query()->whereDate('pay_at', "=", Carbon::now()->subDays(1)->toDate())->sum('total_amount');
        $data[] = OwnerItem::query()->whereDate('pay_at', "=", Carbon::now()->subDays(2)->toDate())->sum('total_amount');
        $data[] = OwnerItem::query()->whereDate('pay_at', "=", Carbon::now()->subDays(3)->toDate())->sum('total_amount');
        $data[] = OwnerItem::query()->whereDate('pay_at', "=", Carbon::now()->subDays(4)->toDate())->sum('total_amount');
        $data[] = OwnerItem::query()->whereDate('pay_at', "=", Carbon::now()->subDays(5)->toDate())->sum('total_amount');
        $data[] = OwnerItem::query()->whereDate('pay_at', "=", Carbon::now()->subDays(6)->toDate())->sum('total_amount');

        // 图表数据
        $this->withChart([
            [
                'name' => '最近七天收入',
                'data' => [$data[0], $data[1], $data[2], $data[3], $data[4], $data[5], $data[6]],
            ],
        ]);
    }

    /**
     * 设置图表数据.
     *
     * @param array $data
     *
     * @return $this
     */
    public function withChart(array $data)
    {
        return $this->chart([
            'series' => $data,
        ]);
    }

    /**
     * 设置卡片内容.
     *
     * @param string $title
     * @param string $value
     * @param string $style
     *
     * @return $this
     */
    public function withContent($title, $value, $style = 'success')
    {
        // 根据选项显示
        $label = strtolower(
            $this->dropdown[request()->option] ?? '上次周'
        );

        $minHeight = '183px';

        $url = route('admin.bill.index');

        return $this->content(
            <<<HTML
<div class="d-flex p-1 flex-column justify-content-between" style="padding-top: 0;width: 100%;height: 100%;min-height: {$minHeight}">
    <div class="text-left">
        <h1 class="font-lg-2 mt-2 mb-0">{$title}</h1>
        <h5 class="font-medium-2" style="margin-top: 10px;">
            <span class="text-{$style}">{$value} </span>
            <span>对比 {$label}</span>
        </h5>
    </div>
    <a href="{$url}" class="btn btn-primary shadow waves-effect waves-light">打开收入流水 <i class="feather icon-chevrons-right"></i></a>
</div>
HTML
        );
    }
}
